#include<bits/stdc++.h>
using namespace std;
/*class Solution {
public:
    string simplifyPath(string path) {
        stack<string> st;
        int left=0;
        int right=0;
        int n=path.size();
        while(left<n&&right<n)
        {
            while(left+1<n&&path[left+1]=='/') left++;
            right=left+1;
            while(right+1<n&&path[right+1]!='/') right++;
            string s=path.substr(left,right-left+1);
            if(s=="/.."&&!st.empty()) st.pop();
            if(s=="/.");
            if(s!="/.."&&s!="/.") st.push(s);
            left=right+1;
        }
        while(!st.empty()&&st.top()=="/") st.pop();
        string s;
        while(!st.empty())
        {
            s=st.top()+s;
            st.pop();
        }
        return s==""?"/":s;
    }
};*/

class Solution {
public:
    string simplifyPath(string path) {
        stringstream ss(path);
        string s;
        stack<string> st;
        while(getline(ss,s,'/'))
        {
            if(s.empty()||s==".") continue;
            if(s!="..") st.push(s);
            else if(!st.empty()) st.pop();
        }
        string ret="";
        while(!st.empty())
        {
            ret=st.top()+ret;
            ret="/"+ret;
            st.pop();
        }
        return ret==""?"/":ret;
    }
};


int main()
{
    string s="/home/user/Documents/../Pictures";
    Solution so;
    so.simplifyPath(s);
    return 0;
}